/*
 * Copyright (c) 2022, 2024 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.service.inject.api;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Function;

import io.helidon.common.config.Config;
import io.helidon.common.config.ConfigException;
import io.helidon.common.types.TypeName;

/**
 * Configuration related types used with Helidon injection.
 */
public final class Configuration {
    private Configuration() {
    }

    /**
     * Configuration property, can be used when using Helidon declarative.
     */
    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.CLASS)
    @Documented
    @Injection.Qualifier
    public @interface Value {
        /**
         * Configuration key (from the config root) to find the value.
         * <p>
         * Default configuration key is {@code <class_name>.<field_name>} for field injection (if supported),
         * and {@code <className>.<constructor_param_name>} for constructor injection. The {@code <class_name>}
         * is the fully qualified class name of the type that uses this annotation.
         * <p>
         * Note that parameter names are not retained for runtime, so the parameter name when not using
         * build time processing would be generated by the JVM.
         * <p>
         * A default value can be specified as part of the key definition, separated by {@code :} (colon) from
         * the key, such as:
         * <pre>
         *     public MyType(@Config.Value("app.greeting:Ciao") String greeting) {
         *     }
         * </pre>
         * This would look in configuration tree for key {@code app.greeting}, and if not found, would use
         * {@code Ciao} as the default value. If default value is not defined, and the property does not exist,
         * an exception would be thrown at runtime.
         * <p>
         * Default values will have string converters applied if the type is not {@link String}.
         * To provide a more complex default, such as when mapping a configuration tree to an object, use
         * {@link #defaultProvider()} instead.
         *
         * @return configuration value key, with possible default value
         */
        String value() default "";

        /**
         * A class that provides the default value
         * of the type expected by the injected field/parameter, it must be accessible through service registry.
         *
         * @return default provider class
         */
        Class<? extends Function<Config, ?>> defaultProvider() default NoProvider.class;

        /**
         * Default value of {@link Value#defaultProvider()}.
         */
        final class NoProvider implements Function<Config, Object> {
            /**
             * Type name of this class.
             */
            public static final TypeName TYPE = TypeName.create(NoProvider.class);

            private NoProvider() {
            }

            @Override
            public Object apply(Config config) {
                throw new ConfigException("This default value provider should not be used");
            }
        }
    }
}
